package viewModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import modelo.entidad.Laboratorio2;
import modelo.entidad.Laboratorio2_Filtro;
import modelo.hibernate.dao.DaoLaboratorio2;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;

public class Laboratorio2ViewModel {

	private static final String labMessage = "Total %d registrados";

	private Laboratorio2_Filtro labFilter = new Laboratorio2_Filtro();

	private DaoLaboratorio2 daoLab = new DaoLaboratorio2();

	List<Laboratorio2> todoLab;

	private static final long serialVersionUID = 1L;
	private boolean displayEdit = true;

	public boolean isDisplayEdit() {
		return displayEdit;
	}

	public Laboratorio2ViewModel() {
		super();
		try {
			todoLab = daoLab.obtenerTodos();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public List<Laboratorio2> getObtenerRegistrados() {
		return todoLab;
	}

	public Laboratorio2_Filtro getLabFilter() {
		return labFilter;
	}

	public String getFooter() {
		return String.format(labMessage, todoLab.size());
	}

	@Command
	@NotifyChange({ "obtenerRegistrados", "footer" })
	public void changeFilter() {
		todoLab = getLabFilter(labFilter);
	}

	@NotifyChange({ "obtenerUsuarios", "displayEdit" })
	public void setDisplayEdit(boolean displayEdit) {
		this.displayEdit = displayEdit;
	}

	@Command
	public void changeEditableStatus(@BindingParam("laboratorio2") Laboratorio2 lab) {
		lab.setEditingStatus(!lab.getEditingStatus());
		refreshRowTemplate(lab);
	}

	@Command
	public void confirm(@BindingParam("laboratorio") Laboratorio2 lab) {
		changeEditableStatus(lab);
		try {
			daoLab.actualizarLaboratorio2(lab);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		refreshRowTemplate(lab);
	}

	public void refreshRowTemplate(Laboratorio2 lab) {
		/*
		 * This code is special and notifies ZK that the bean's value has
		 * changed as it is used in the template mechanism. This stops the
		 * entire Grid's data from being refreshed
		 */
		BindUtils.postNotifyChange(null, null, lab, "editingStatus");
	}

	@Command
	@NotifyChange({ "obtenerRegistrados", "footer" })
	public void eliminarRegistro(@BindingParam("laboratorio") Laboratorio2 es) {
		try {
			todoLab.remove(es);
			daoLab.eliminarLaboratorio2(es);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<Laboratorio2> getLabFilter(Laboratorio2_Filtro labFilter) {

		List<Laboratorio2> someLab = new ArrayList<Laboratorio2>();
		String desc = labFilter.getDescripcion();
		String def = labFilter.getDefensa();


		List<Laboratorio2> labAll = null;
		try {
			labAll = daoLab.obtenerTodos();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (Iterator<Laboratorio2> i = labAll.iterator(); i.hasNext();) {
			Laboratorio2 tmp = i.next();
			if (tmp.getDescripcion().toLowerCase().contains(desc)
					&& tmp.getDefensa().toLowerCase().contains(def))
			 {
				someLab.add(tmp);
			}
		}
		return someLab;
	}
	
	


}
